//
//  DZFileManager.m
//  DZFoundation
//
//  Created by Darren Zheng on 15/10/13.
//  Copyright © 2015年 Darren Zheng. All rights reserved.
//

#import "DZFileManager.h"

@implementation DZFileManager

+ (DZFileManager *)sharedInstance
{
    static DZFileManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (NSString *)documentDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}

- (NSString *)documentFilePathWithName:(NSString *)strName
{
    return [[self documentDirectory] stringByAppendingPathComponent:strName];
}

- (NSString *)bundleFilePathWithName:(NSString *)strName
{
    return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:strName];
}

- (NSString *)cacheDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
}

- (NSString *)cacheFilePathWithName:(NSString *)strName
{
    return [[self cacheDirectory] stringByAppendingPathComponent:strName];
}

- (BOOL)fileExistAtCacheDirectoryWithName:(NSString *)strName
{
    
//    NSLog(@"path = %@",[self cacheDirectory]);
    return [[NSFileManager defaultManager] fileExistsAtPath:[[self cacheDirectory] stringByAppendingPathComponent:strName]];
}

- (BOOL)copyBundleItemWithFileName:(NSString *)strBundleFileName toCacheDirectoryWithFileName:(NSString *)strCacheFileName
{
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSLog(@"%@",[self bundleFilePathWithName:strBundleFileName]);
    NSLog(@"%@",[self cacheFilePathWithName:strCacheFileName]);
    NSLog(@"%d",[fileManager copyItemAtPath:[self bundleFilePathWithName:strBundleFileName] toPath:[self cacheFilePathWithName:strCacheFileName] error:&error]);

    return [fileManager copyItemAtPath:[self bundleFilePathWithName:strBundleFileName] toPath:[self cacheFilePathWithName:strCacheFileName] error:&error];
}

@end
